//
//  SRInputView.m
//  ComProp
//
//  Created by 孙锐 on 2018/1/9.
//  Copyright © 2018年 孙锐. All rights reserved.
//

#import "SRInputView.h"



@implementation SRInputView{
    NSString *oldString;
}

-(void)creatView{
    
    CGFloat width = self.bounds.size.height-self.borderWidth*2;
    CGFloat border = (self.bounds.size.width-8*self.borderWidth-4*width)/3;
    for (int i=0; i<self.number; i++) {
        UITextField *oneLabel = [[UITextField alloc]init];
        oneLabel.tag = 900+i;
        oneLabel.layer.borderColor = self.borderColor.CGColor;
        oneLabel.layer.borderWidth = self.borderWidth;
        oneLabel.frame = CGRectMake(self.borderWidth+(border+width)*i, 0, width, self.bounds.size.height-self.borderWidth*2);
        oneLabel.layer.cornerRadius = 10*ScreenScale;
        oneLabel.backgroundColor = self.contentColor;
        oneLabel.textAlignment = NSTextAlignmentCenter;
        oneLabel.enabled = NO;
        oneLabel.font = [UIFont systemFontOfSize:self.fontSize];
        oneLabel.textColor = self.textColor;
        [self addSubview:oneLabel];
    }
    
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(textFieldDidChange) name:UITextFieldTextDidChangeNotification object:self.textField];
}

-(void)textFieldDidChange{
    NSString *text = self.textField.text;
    if (text.length<=self.number) {
        oldString = self.textField.text;
        if (text.length == self.number) {
            [self.textField resignFirstResponder];
        }
    }else{
        self.textField.text = oldString;
        text = oldString;
    }
    NSLog(@"%@",text);

    for (int i=0; i<self.number; i++) {
        UITextField *textf = [self viewWithTag:900+i];
        if (i<text.length) {
            textf.text = [text substringWithRange:NSMakeRange(i, 1)];
        }else{
            textf.text = @"";
        }
    }
}


@end
